package com.rokyinfo.pdm.controller.v1;

import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DModel;
import com.rokyinfo.pdm.service.IDModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备型号表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-21
 */
@Api(value = "/model", tags = "Model", description = "设备型号")
@RestController
@RequestMapping(value = BasicPath.V1 + "/models")
public class DModelController {
    @Autowired
    private IDModelService mIDModelService;

    @ApiOperation(value = "获取设备型号列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<List<DModel>> getDModelList() throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        return ResponseEntity.ok(mIDModelService.selectByMap(null));
    }

    @ApiOperation(value = "新建设备型号", notes = "根据dModel对象创建设备型号")
    @ApiImplicitParam(name = "dModel", value = "设备型号实体DModel", required = true, dataType = "DModel")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDModel(@RequestBody DModel dModel) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDModelService.insert(dModel)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取设备型号信息", notes = "根据url的id来获取设备型号信息")
    @ApiImplicitParam(name = "id", value = "设备型号ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<DModel> getDModel(@PathVariable Long id) {
        DModel mDModel = mIDModelService.selectById(id);
        if (mDModel != null) {
            return ResponseEntity.ok(mDModel);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新设备型号", notes = "根据url的id来指定更新对象，并根据传过来的dModel信息来更新设备型号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备型号ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "dModel", value = "设备型号实体DModel", required = true, dataType = "DModel")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity putDModel(@PathVariable Long id, @RequestBody DModel dModel) {
        dModel.setModelId(id);
        if (mIDModelService.updateById(dModel)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除设备型号", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "设备型号ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDModel(@PathVariable Long id) {
        if (mIDModelService.deleteById(id)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
